package com.repast.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.repast.common.ServerResponse;
import com.repast.service.IAddDishService;
import com.repast.vo.AddDishItemVo;
import com.repast.vo.AddDishOrders;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Controller
@RequestMapping("/addDish/")
public class AddDishController {

	@Autowired
	private IAddDishService addDishService;
	
	@RequestMapping("addDishOrders.do")
	@ResponseBody
	public ServerResponse addDishOrders(String json) throws Exception{
		AddDishOrders addDishOrders = new AddDishOrders();
		JSONObject jsonStr = JSONObject.fromObject(json);
		addDishOrders.setOrdersId(jsonStr.getInt("ordersId"));
		JSONArray jsonArray = jsonStr.getJSONArray("addDishItemList");
		List<AddDishItemVo> list = jsonArray.toList(jsonArray, AddDishItemVo.class);
		addDishOrders.setAddDishItemList(list);
		return addDishService.addDish(addDishOrders);
	}
	
	//支付成功后需要重新计算订单的总额
	@RequestMapping("success.do")
	@ResponseBody
	public ServerResponse afterSuccess(Integer ordersId,String AddDishTotal){
		return addDishService.recountOrdersTotal(ordersId,new Double(AddDishTotal));
	}
}
